<template>
	<view>
		<view class="job-dtl-box">
			<view class="dtl-box">
				<view class="job-box">
					<view class="tit">公司名称</view>
					<view class="dtl-input-box">
						<input :class="{'tips-txt': !detail.company_name}" placeholder="请输入公司名称" v-model="detail.company_name"></input>
					</view>
				</view>
				<view class="job-box">
					<view class="tit">在职时间</view>
					<view class="dtl-time-box">
						<picker mode="date" data-name="startDate" fields="month" :value="detail.time.startDate" @change="onPickerChange">
							<view :class="{'time time-left': true, 'tips-txt': !detail.time.startDate}">{{detail.time.startDate || '入职时间'}}</view>
						</picker>
						<text class="jiange">—</text>
						<picker mode="date" data-name="endDate" fields="month" :value="detail.time.endDate" @change="onPickerChange">
							<view :class="{'time time-right': true, 'tips-txt': detail.time.endDate}">{{ detail.time.endDate || '离职时间' }}</view>
						</picker>
					</view>
				</view>
				<view class="job-box">
					<view class="tit">工作岗位</view>
					<view class="dtl-input-box">
						<input maxlength="8" :class="{'tips-txt': !detail.last_post}" placeholder="请输入" v-model="detail.last_post"></input>
					</view>
				</view>
				<view class="job-box">
					<view class="tit">工作内容</view>
					<navigator hover-class="hoverClass2" data-key="jobContent" @click="sendTxtFn" url="/pages/recruit/edit-self-ev/index?type=jobContent">
						<view :class="{'tips-txt': !jobContent}">
							<view class="txtHidden txt-view">
								{{ jobContent || '请输入'}}
							</view>
							<text class="iconfontPT iconZp-toright pull-right"></text>
						</view>
					</navigator>
				</view>
			</view>
		</view>
		<btnTpl is="btn-tpl" @confirmEvent="submitDataFn">保存</btnTpl>
	</view>
</template>

<script>
	import btnTpl from '../../components/recruit/btn-tpl.vue'
	import {
		getWorkDtlInfo,
		setWorkDtlInfo
	} from '../../../apis/recruit/index.js'

	export default {
		data() {
			return {
				jobContent: '',
				detail: {
					company_name: '',
					time: {
						startDate: '',
						endDate: ''
					},
					last_post: '',
					salary: '',
					job_content: ''
				},
				rules: {
					company_name: [{
						required: true,
						message: '请输入公司名称',
					}],
					"time.startDate": [{
						required: true,
						message: '请选择入职时间',
					}],
					"time.endDate": [{
						required: true,
						message: '请选择离职时间',
					}],
					last_post: [{
							required: true,
							message: '请输入工作岗位',
						},
						{
							min: 2,
							max: 20,
							message: '工作岗位字符长度在 2 到 20 个字符',
						}
					],
					job_content: [{
						required: true,
						message: '请输入工作内容',
					}, {
						min: 10,
						max: 140,
						message: '工作内容字符长度在 10 到 140 个字符',
					}],
				}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id

				this.initDataFn();
			}
		},
		components: {
			btnTpl
		},
		methods: {
			initDataFn() {
				let _this = this;
				getWorkDtlInfo({
					id: _this.id
				}).then(res => {
					if (res.code == 1) {

						res.data.time = {
							startDate: res.data.s_serving,
							endDate: res.data.e_serving,
						}

						_this.detail = res.data
						delete _this.detail.s_serving
						delete _this.detail.e_serving
						_this.jobContent = res.data.job_content
					}
				})
			},
			//提交求职期望
			submitDataFn() {
				let _this = this,
					params = this.detail,
					rules = this.rules;
				params.job_content = this.jobContent;
				params.s_serving = this.detail.time.startDate
				params.e_serving = this.detail.time.endDate
				delete params.time.startDate
				delete params.time.endDate
				this.$verification(params, rules, ok => {
					if (ok) {
						if (this.$moment(params.time.startDate).isAfter(params.time.endDate)) {
							return uni.showToast({
								title: '入职时间应在离职时间之前',
								icon: 'none',
								duration: 1500,
								mask: true,
							});
						}
						this.$loading.show()
						setWorkDtlInfo(params).then(res => {
							if (res.code == 1) {
								this.$loading.hide()

								uni.showToast({
									title: _this.id ? '修改成功' : '提交成功',
									icon: 'success',
									duration: 2000,
									mask: true,
								});
								setTimeout(() => {
									var pages = getCurrentPages();
									if (pages.length > 1) {
										var prePage = pages[pages.length - 2];
										prePage.$vm.getWorkDtlInfo() // 123
									}
									uni.navigateBack({
										delta: 1
									})
								}, 2000)
							}
						})
					}
				})
			},
			onPickerChange(e) {
				let name = e.currentTarget.dataset.name,
					value = e.detail.value

				this.detail.time[name] = value

			},
			sendTxtFn(e) {
				let key = e.currentTarget.dataset.key;
				uni.setStorageSync(key, this[key])
			},
		}
	}
</script>

<style lang="less">
	.job-dtl-box {
		padding: 0 30rpx 0;
		border-top: 1rpx solid #C7BEBE;
	}

	.tips-txt {
		color: #989898;
	}

	.dtl-box {
		margin-top: 22rpx;
	}

	.job-box {
		padding: 38rpx 0 0;
		box-shadow: 0 1rpx 0.5rpx -1rpx #CCCCCC;

		.tit {
			font-size: 24rpx;
			line-height: 24rpx;
		}

		.txt-view {
			display: inline-block;
			width: 90%;
		}
	}

	.job-box navigator,
	.job-box .dtl-input-box,
	.dtl-time-box {
		font-size: 30rpx;
		line-height: 30rpx;
		padding-top: 18rpx;
		padding-bottom: 38rpx;
	}

	.iconZp-toright {
		font-size: 24rpx;
		color: #999;
	}

	.dtl-time-box view,
	.job-box picker {
		display: inline-block;
		width: 280rpx;
		text-align: center;
	}

	.jiange {
		display: inline-block;
		width: 120rpx;
		text-align: center;
	}
</style>
